package top.littlejiang.onlinexam.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import top.littlejiang.onlinexam.common.annotation.Log;
import top.littlejiang.onlinexam.common.base.BasePage;
import top.littlejiang.onlinexam.common.utils.Result;
import top.littlejiang.onlinexam.entity.OlexamQuestions;
import top.littlejiang.onlinexam.service.OlexamQuestionsService;
import top.littlejiang.onlinexam.vo.QuestionQueryVO;

import java.io.Serializable;
import java.util.List;

/**
 * @author chen
 * @Description
 * @since 2021/3/16 11:33
 */
@Controller
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    OlexamQuestionsService questionsService;

    /**
     * 分页查询
     * @param page
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    @RequiresPermissions("olexam:exampaper:question")
    public Result getList(@RequestBody BasePage<QuestionQueryVO> page){
        IPage<OlexamQuestions> pages = questionsService.getQuestionListPage(page);
        return Result.ok(pages);
    }

    /**
     * 禁用
     * @param ids
     * @return
     */
    @Log("禁用题目")
    @PostMapping("/del")
    @ResponseBody
    @RequiresPermissions("olexam:exampaper:question")
    public Result delete(@RequestBody List<Serializable> ids){
        questionsService.logicDeleteById(ids);
        return Result.ok();
    }

    /**
     * 保存或更新
     * @param question
     * @param files
     * @return
     */
    @Log("保存/更新题目")
    @PostMapping("/insertorupdate")
    @ResponseBody
    @RequiresPermissions("olexam:exampaper:question")
    public Result insertOrUpdate(OlexamQuestions question, MultipartFile[] files){
        questionsService.insertOrUpdate(question, files);
        return Result.ok();
    }

    /**
     * 根据条件查询所有
     * @param queryVO
     * @return
     */
    @PostMapping("/queryall")
    @ResponseBody
    @RequiresPermissions("olexam:exampaper:question")
    public Result queryAll(@RequestBody QuestionQueryVO queryVO){
        List<OlexamQuestions> rst = questionsService.queryAll(queryVO);
        return Result.ok(rst);
    }

    /**
     * 根据id 查询 不包含答案
     * @param ids
     * @return
     */
    @PostMapping("/listbyid")
    @ResponseBody
    public Result listByIds(@RequestBody List<Serializable> ids){
        return Result.ok(questionsService.selectInIds(ids, false));
    }

    /**
     * 根据id 查询 包括答案
     * @param ids
     * @return
     */
    @PostMapping("/answerbyid")
    @ResponseBody
    @RequiresPermissions("olexam:exampaper:question")
    public Result answerByIds(@RequestBody List<Serializable> ids){
        return Result.ok(questionsService.selectInIds(ids, true));
    }
}
